package com.ruoyi.project.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.FieldConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.blog.domain.BTags;
import com.ruoyi.project.blog.domain.vo.SystemTagListVo;
import com.ruoyi.project.blog.mapper.BTagsMapper;
import com.ruoyi.project.blog.service.IBTagsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 博客标签Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Service
public class BTagsServiceImpl extends ServiceImpl<BTagsMapper, BTags> implements IBTagsService {
    @Autowired
    private BTagsMapper bTagsMapper;

    /**
     * 查询博客标签
     *
     * @param id 博客标签主键
     * @return 博客标签
     */
    @Override
    public BTags selectBTagsById(Long id) {
        return bTagsMapper.selectBTagsById(id);
    }

    /**
     * 查询博客标签列表
     *
     * @param bTags 博客标签
     * @return 博客标签
     */
    @Override
    public List<SystemTagListVo> selectBTagsList(BTags bTags) {
        return bTagsMapper.selectBTagsList(bTags);
    }

    /**
     * 新增博客标签
     *
     * @param bTags 博客标签
     * @return 结果
     */
    @Override
    public AjaxResult insertBTags(BTags bTags) {
        validateName(bTags.getName());
        bTags.setCreateTime(DateUtils.getNowDate());
        bTagsMapper.insertBTags(bTags);
        return AjaxResult.success();
    }

    /**
     * 修改博客标签
     *
     * @param bTags 博客标签
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateBTags(BTags bTags) {
        bTags.setUpdateTime(DateUtils.getNowDate());
        BTags entity = bTagsMapper.selectById(bTags.getId());
        if (!entity.getName().equalsIgnoreCase(bTags.getName())) {
            validateName(bTags.getName());
        }
        baseMapper.updateById(bTags);
        return AjaxResult.success();
    }

    /**
     * 批量删除博客标签
     *
     * @param ids 需要删除的博客标签主键
     * @return 结果
     */
    @Override
    public int deleteBTagsByIds(Long[] ids) {
        return bTagsMapper.deleteBTagsByIds(ids);
    }

    /**
     * 删除博客标签信息
     *
     * @param id 博客标签主键
     * @return 结果
     */
    @Override
    public int deleteBTagsById(Long id) {
        return bTagsMapper.deleteBTagsById(id);
    }

    @Override
    public List<SystemTagListVo> listTags(String name) {

        List<SystemTagListVo> list = bTagsMapper.listTags(name);
        return list;
    }

    @Override
    public ResponseResult selectTagList(Long userId) {
        LambdaQueryWrapper<BTags> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(BTags::getUserId, userId);
        }
        queryWrapper.orderByDesc(BTags::getSort);
        List<BTags> tags = bTagsMapper.selectList(queryWrapper);
        return ResponseResult.success(tags);
    }

    public void validateName(String name) {
        BTags entity = baseMapper.selectOne(new QueryWrapper<BTags>().eq(FieldConstants.NAME, name));
        if (ObjectUtils.isNotEmpty(entity)) {
            throw new BusinessException("标签名已存在");
        }
    }
}
